package com.example.dessert.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.dessert.dao.FavoriteMapper;
import com.example.dessert.entity.Favorite;
import com.example.dessert.service.FavoriteService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName: FavoriteServiceImpl
 * @Description:
 * @Author li
 * @Date 2022/1/14
 * @Version 1.0
 */
@Service
public class FavoriteServiceImpl implements FavoriteService {

    @Resource
    private FavoriteMapper favoriteMapper;


    @Override
    public Favorite queryFavoriteBuyUserId(Long userId,Long productId) {

        QueryWrapper<Favorite> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId)
               .eq("product_id",productId);
        return favoriteMapper.selectOne(wrapper);

    }

    @Override
    public Long storeFavorite(Favorite favorite) {

        favoriteMapper.insert(favorite);
        return  favorite.getId();

    }

    @Override
    public int cancelFavorite(Long id) {

        return favoriteMapper.deleteById(id);

    }

    @Override
    public List<Favorite> queryFavoriteList(Long user_id) {

        return favoriteMapper.queryFavoriteList(user_id);

    }

    @Override
    public List<Favorite> queryFavoritePage(Integer startPage, Integer pageSize, Long user_id) {

        return favoriteMapper.queryFavoritePage(startPage,pageSize,user_id);

    }


    @Override
    public Long queryFavoriteCount(Long userId) {

        QueryWrapper<Favorite> wrapper = new QueryWrapper<>();
        if(userId != null){
            wrapper.eq("user_id",userId);
        }
        return favoriteMapper.selectCount(wrapper);

    }

    @Override
    public int deleteFavoriteBuyId(Long id) {

        return favoriteMapper.deleteById(id);

    }






}
